package com.wmd.core.util;


import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Simple to Introduction
 *
 * @projectName:scs-platform
 * @packageName:com.wmd.iot.badge.util
 * @className:WrapperGenerator
 * @Description:[一句话描述该类的功能]
 * @Author:luyincheng
 * @CreateDate:2021/1/11 9:55
 * @blog:[方法具体上行/下行参数文档]
 */
public class HumpTransform {

    /*    public static Wrapper GeneratorWrapperByMap(Map<String,String> queryMap){
            QueryWrapper wrapper=new QueryWrapper();
            queryMap.forEach((k,v)->{
                wrapper.eq(humpToLine2(k),v);
            });
            return wrapper;
        }*/
    public static Map<String, String> HumpMapToLineMap(Map<String, String> map) {
        Map newMap = new HashMap<String, String>();
        map.forEach((k, v) -> {
            newMap.put(humpToLine2(k), v);
        });
        return newMap;
    }

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * 下划线转驼峰
     */
    public static String lineToHump(String str) {
//        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    public static String lineToHumpCompletely(String str) {
        str = str.toLowerCase();
        return lineToHump(str);
    }

    /**
     * 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)})
     */
    public static String humpToLine(String str) {
        return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /**
     * 驼峰转下划线,效率比上面高
     */
    public static String humpToLine2(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            if(matcher.start()==0){
                matcher.appendReplacement(sb,matcher.group(0).toLowerCase());
            }else {
                matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
            }

        }
        matcher.appendTail(sb);

        return sb.toString();
    }
}
